package tech.aistar.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.pojo.Railticket;
import tech.aistar.pojo.Result;
import tech.aistar.pojo.Train;
import tech.aistar.pojo.User;
import tech.aistar.service.RailticketService;
import tech.aistar.service.TrainService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 本类用来演示: 购物车的控制层
 *
 * @author: success
 * @date: 2021/6/23 8:23 上午
 */
@Controller
@RequestMapping("/train")
public class TrainController {
    //controller层调用service层
    @Autowired
    private RailticketService railticketService;

    @Autowired
    private TrainService trainService;

    /**
     * 添加到购物车
     * @param id 页面传过来的产品的id
     * @return
     */
    @GetMapping("/add")
    @ResponseBody
    public Result addTrain(Integer id, HttpServletRequest req){
        //设置购物的拥有者user_id
        //因为登录之后,user存储在session作用域中,从作用域中得到这个登录的User对象的
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        //1. 根据id查询单个product
        Railticket railticket = railticketService.getById(id);

        //校验user_id和product_id在购物车表中是否同时具备唯一性
        //如果某个人已经添加了某个产品的话,后续的添加的动作,更新count+1
        //涉及到条件查询
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("railticket_id",id).eq("user_id",user.getId());

        Train c = trainService.getOne(queryWrapper);

        if(c!=null){ // 曾经已经添加过...
            UpdateWrapper<Train> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("railticket_id",id).eq("user_id",user.getId());

            c.setCounts(c.getCounts()+1);

            c.setPrice(railticket.getPrice()*c.getCounts());

            //进行更新操作
            boolean b = trainService.update(c,updateWrapper);

            if(b){
                return new Result("200", "购票成功", null);
            }

            return new Result("500", "购票失败", null);
        }

        //头一次添加..
        //2. 创建Train对象
        Train train = new Train();
        train.setRailticketId(id);
        train.setTrainTrainNum(railticket.getTrainNum());
        train.setTrainDepartureSta(railticket.getDepartureSta());
        train.setTrainTerminalSta(railticket.getTerminalSta());
        train.setRailticketPrice(railticket.getPrice());//票的单价
        train.setCounts(1);//加入购车的数量...
        train.setPrice(railticket.getPrice()*train.getCounts());//票的总价


        train.setUserId(user.getId());



        //end - 调用保存方法

        boolean flag = trainService.save(train);

        if(flag) {
            return new Result("200", "购票成功", null);
        }
        return new Result("500", "购票失败", null);
    }


    @GetMapping("/index")
    public String index(HttpServletRequest req){
        //构建一个session对象
        HttpSession session = req.getSession();

        //从session作用域中获取user对象
        User user = (User) session.getAttribute("user");

        //根据user_id来查询购物车列表
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());

        //查询购物车表中的所有数据并且加载出来.
        List<Train> trainList = trainService.list(queryWrapper);

        //将这个集合对象放入到request作用域中
        session.setAttribute("trainList",trainList);

        //转发到/cart/index.jsp
        return  "train/index";

    }
    /**
     * 同步删除 - 每次进行删除操作的时候,还需要进行查询一遍.
     * @param id
     * @return
     */
    @GetMapping("/del")
    public String del(Integer id){
        //根据id进行删除操作
        trainService.removeById(id);
        //思考跳转
        return "redirect:/train/index";
    }

    /**
     * 异步删除
     * @param id
     * @return
     */
    @GetMapping("/ajaxDel")
    @ResponseBody
    public Result del2(Integer id){
        boolean flag = trainService.removeById(id);
        if(flag)
            return new Result("200","删除成功!",null);
        return new Result("500","删除失败!",null);
    }

    /**
     * 什么时候在参数中可以使用对象来接受
     * 当前端传过来的key的名称和cart对象中的属性的名称保持一致的时候,那么
     * 参数就可以使用对象来接受
     *
     * Integer id,Integer counts - "摆地摊的方式",适用于参数少的情况
     *
     * @param train
     * @return
     */
    @GetMapping("/update")
    @ResponseBody
    public Result update(Train train){//封装了id以及counts
        //根据id进行一个更新操作
        //根据id查询这个cart在db中的行记录
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();

        Train c = trainService.getById(train.getId());
        //重新设置它需要更新的属性
        c.setCounts(train.getCounts());
        c.setPrice(c.getRailticketPrice()*train.getCounts());

        trainService.updateById(c);

        return new Result("200","更新成功",null);
    }

}
